<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">演示</a>
                <a>
                    <cite>导航元素</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <button class="layui-btn" onclick="xadmin.open('添加会员权限','/admin/rule/create',600,610)"><i class="layui-icon"></i>添加</button>
                </div>

                <div class="layui-card-body ">
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>权限名称</th>
                            <th>权限规则</th>
                            <th>图标</th>
                            <th>备注</th>
                            <th>权重</th>
                            <th>状态</th>
                            <th>操作</th>
                        </thead>
                        <tbody class="x-cate">
                        {{range .treeMenus}}
                            <tr cate-id='{{.Id}}' fid='{{.Pid}}' >
                                <td>{{.Id}}</td>
                                <td>
                                    <i class="layui-icon x-show" status="false">&#xe623;</i>
                                    {{.Name}}
                                </td>
                                <td>{{.SkipUrl}}</td>
                                <td><i class="iconfont">{{str2html .Icon}}</i></td>
                                <td>{{.Remark}}</td>
                                <td>{{.Weigh}}</td>
                                <td><input type="checkbox" name="status" lay-text="开启|停用" {{if compare .Status 1}} checked="" {{end}} lay-skin="switch"  lay-filter="status" data-id="{{.Id}}"></td>
                                <td class="td-manage">
                                    <button class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('编辑会员权限','/admin/rule/edit/{{.Id}}',600,610)" ><i class="layui-icon">&#xe642;</i>编辑</button>
                                    <button class="layui-btn-danger layui-btn layui-btn-xs"  onclick="del(this,{{.Id}})" href="javascript:;" ><i class="layui-icon">&#xe640;</i>删除</button>
                                </td>
                            </tr>
                            {{range $elem := .Children}}
                                <tr cate-id='{{$elem.Id}}' fid='{{$elem.Pid}}' >
                                    <td>{{$elem.Id}}</td>
                                    <td>&nbsp;&nbsp;&nbsp;&nbsp;├&nbsp;{{$elem.Name}}</td>
                                    <td>{{$elem.SkipUrl}}</td>
                                    <td><i class="iconfont">{{str2html .Icon}}</i></td>
                                    <td>{{$elem.Remark}}</td>
                                    <td>{{$elem.Weigh}}</td>
                                    <td><input type="checkbox" name="status" lay-text="正常|禁用" {{if compare $elem.Status 1}} checked="" {{end}} lay-skin="switch" lay-filter="status" data-id="{{.Id}}"></td>
                                    <td class="td-manage">
                                        <button class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('编辑会员权限','/admin/rule/edit/{{.Id}}',600,610)" ><i class="layui-icon">&#xe642;</i>编辑</button>
                                        <button class="layui-btn-danger layui-btn layui-btn-xs"  onclick="del(this,{{.Id}})" href="javascript:;" ><i class="layui-icon">&#xe640;</i>删除</button>
                                    </td>
                                </tr>
                            {{end}}
                        {{end}}
                        </tbody>
                    </table>
                </div>
                <div class="layui-card-body ">

                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form'], function(){
        form = layui.form;

        form.on('switch(status)', function (obj) {
            var id = $(this).data('id')
            var status = obj.elem.checked?'1':'2';
            request_post('/admin/rule/update_status',{id:id,status:status}).then(function (data) {
                //设置成功
            },function (error) {
                msg_error(error.msg)
            })

        });

    });



    /*删除*/
    function del(obj,id){
        layer.confirm("确认要执行此操作吗？",function(index){
            request_get('/admin/rule/destroy/'+id).then(function (data) {
                $(obj).parents("tr").remove();
            },function (error) {
                msg_error(error.msg)
            })
            layer.close(index)
        })
    }

    // 分类展开收起的分类的逻辑
    //
    $(function(){
        // $("tbody.x-cate tr[fid!='0']").hide();
        // 栏目多级显示效果
        $('.x-show').click(function () {
            if($(this).attr('status')=='true'){
                $(this).html('&#xe625;');
                $(this).attr('status','false');
                cateId = $(this).parents('tr').attr('cate-id');
                $("tbody tr[fid="+cateId+"]").show();
            }else{
                cateIds = [];
                $(this).html('&#xe623;');
                $(this).attr('status','true');
                cateId = $(this).parents('tr').attr('cate-id');
                getCateId(cateId);
                for (var i in cateIds) {
                    $("tbody tr[cate-id="+cateIds[i]+"]").hide().find('.x-show').html('&#xe623;').attr('status','true');
                }
            }
        })
    })

    var cateIds = [];
    function getCateId(cateId) {
        $("tbody tr[fid="+cateId+"]").each(function(index, el) {
            id = $(el).attr('cate-id');
            cateIds.push(id);
            getCateId(id);
        });
    }

</script>